/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.hcht.xrlh.qiye.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.hcht.xrlh.qiye.entity.Qiye;
import com.hcht.xrlh.qiye.service.QiyeService;

/**
 * 企业信息Controller
 * @author devil
 * @version 2016-12-02
 */
@Controller
@RequestMapping(value = "${adminPath}/qiye/qiye")
public class QiyeController extends BaseController {

	@Autowired
	private QiyeService qiyeService;
	
	@ModelAttribute
	public Qiye get(@RequestParam(required=false) String id) {
		Qiye entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = qiyeService.get(id);
		}
		if (entity == null){
			entity = new Qiye();
		}
		return entity;
	}
	
	@RequiresPermissions("qiye:qiye:view")
	@RequestMapping(value = {"list", ""})
	public String list(Qiye qiye, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Qiye> page = qiyeService.findPage(new Page<Qiye>(request, response), qiye); 
		model.addAttribute("page", page);
		return "xrlh/qiye/qiyeList";
	}

	@RequiresPermissions("qiye:qiye:view")
	@RequestMapping(value = "form")
	public String form(Qiye qiye, Model model) {
		model.addAttribute("qiye", qiye);
		return "xrlh/qiye/qiyeForm";
	}

	@RequiresPermissions("qiye:qiye:edit")
	@RequestMapping(value = "save")
	public String save(Qiye qiye, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, qiye)){
			return form(qiye, model);
		}
		qiyeService.save(qiye);
		addMessage(redirectAttributes, "保存企业信息成功");
		return "redirect:"+Global.getAdminPath()+"/qiye/qiye/?repage";
	}
	
	@RequiresPermissions("qiye:qiye:edit")
	@RequestMapping(value = "delete")
	public String delete(Qiye qiye, RedirectAttributes redirectAttributes) {
		qiyeService.delete(qiye);
		addMessage(redirectAttributes, "删除企业信息成功");
		return "redirect:"+Global.getAdminPath()+"/qiye/qiye/?repage";
	}

}